<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/2/29
 * Time: 21:10
 */
namespace Admin\Controller;
use Think\Controller;

/**
 * array $req=I('param.') 用户提交的数据，分解成$param,$search,$page
 * string $model=CONTROLLER_NAME 模型类名
 * string $action=__FUNCTION__ 模型里handle方法的条件，一般就是当前方法名
 * array $param=$this->getParam() 由$req分解而来，默认为空或空数组
 * array $where=$this->getWhere() 由$param,$search合并而成
 * mixed $field=$this->order 用户自定义过滤字段，默认为空或空数组
 * mixed $order=$this->order 查询数据时的排序规则，默认为空或空数组
 * mixed $page=$this->getPage() 由$req分解而来，默认为空或空数组
 * Class Controller
 * @package IDCU
 */
class BaseController extends Controller
{
    protected $model = CONTROLLER_NAME;

    protected $field = array();

    protected $order = array();

    protected $key = array(
        'page' => array('pageIndex'=>1,'pageSize'=>20),
        'search' => array(
            'select' => array()
        )
    );

    public function index(){
        $this->display();
    }

    public function create(){
        $this->display();
    }

    public function modify(){
        $this->display();
    }

    public function read(){
        if(!IS_GET){
            $res['stat'] = 0;
            $res['msg'] = '请不要通过恶意攻击！';
        }else{
            $where = $this->getWhere();
            $result = D($this->model)->handle('find',$where,$param='',$this->field,$this->order,$this->getPage());
            $res = $result;
            $this->ajaxReturn($res);
        }
    }

    public function search(){
        if(!IS_POST){
            $res['stat'] = 0;
            $res['msg'] = '请不要通过恶意攻击！';
        }else{
            $where = $this->getWhere();
            $result = D($this->model)->handle('select',$where,$param='',$this->field,$this->order,$this->getPage());
            $res = $result;
            $res['recordsTotal'] = $result['count'];
            $res['recordsFiltered'] = $result['count'];
            $this->ajaxReturn($res);
        }
    }

    public function add(){
        if(!IS_POST){
            $res['stat'] = 0;
            $res['msg'] = '请不要通过恶意攻击！';
        }else{
            $param = $this->getParam();
            $result = D($this->model)->handle(__FUNCTION__,$where='',$param,$this->field,$this->order,$this->getPage());
            $res = $result;
            $this->ajaxReturn($res);
        }
    }

    public function update(){
        if(!IS_POST){
            $res['stat'] = 0;
            $res['msg'] = '请不要通过恶意攻击！';
        }else{
            $param = $this->getParam();
            $result = D($this->model)->handle('save',$where='',$param,$this->field,$this->order,$this->getPage());
            $res = $result;
            $this->ajaxReturn($res);
        }
    }

    public function delete(){
        if(!IS_POST){
            $res['stat'] = 0;
            $res['msg'] = '请不要通过恶意攻击！';
        }else{
            $where = $this->getParam();
            $result = D($this->model)->handle(__FUNCTION__,$where,$param='',$this->field,$this->order,$this->getPage());
            $res = $result;
            $this->ajaxReturn($res);
        }
    }

    public function result($action){
        $where = $this->getWhere();
        $field = $this->field;
        $order = $this->order;
        $page = $this->getPage();
        $result = D($this->model)->handle($action,$where,$field,$order,$page);
        return $result;
    }

    protected function getReq(){
        $req = I('param.');
        return $req;
    }

    protected function getParam(){
        $req = $this->getReq();
        foreach($this->key as $k1=>$v1){
            foreach($v1 as $k2=>$v2){
                unset($req[$k2]);
            }
        }
        $param = $req;
        return $param;
    }

    protected function getPage(){
        $req = $this->getReq();
        foreach($req as $k=>$v){
            if(array_key_exists($k,$this->key['page'])){
                $page[$k] = $v ? $v : $this->key['page'][$k];
            }
        }
        return $page;
    }

    protected function getSearch(){
        $req = $this->getReq();
        foreach($req as $k=>$v){
            if(array_key_exists($k,$this->key['search'])){
                $search[$k] = $v;
            }
        }
        return $search;
    }

    protected function getParamWhere(){
        $param = $this->getParam();
        foreach($param as $k=>$v){
            if(strpos($v,',')){
                $paramWhere[$k] = array('in',explode(',',$v));
            }else{
                $paramWhere[$k] = array('eq',$v);
            }
        }
        $paramWhere = $paramWhere ? $paramWhere : array();
        return $paramWhere;
    }

    protected function getSearchWhere(){
        $search = $this->getSearch();
        $searchWhere = array();
        foreach($search as $k1=>$v1){
            $i = 0;
            if($v1 != ''){
                if(array_key_exists($k1,$this->key['search'])){
                    $j = 0;
                    $where = array();
                    foreach($this->key['search'][$k1] as $k2=>$v2){
                        preg_match($v2['regex'],$v1,$match);
                        if($match){
                            if($v2['exp'] == 'like'){
                                $where[$k2] = array($v2['exp'],'%'.$v1.'%');
                            }else{
                                $where[$k2] = array($v2['exp'],$v1);
                            }
                            $j++;
                        }
                    }
                }
                if($j > 1){
                    $where['_logic'] = 'or';
                    $i++;
                }
            }
            if($i == 0){ //没有or的情况
                foreach($where as $k=>$v){
                    $searchWhere[$k] = $v;
                }
            }elseif($i == 1) { //有or的情况（多个组合查询后续再写）
                $searchWhere['_complex'] = $where;
            }
        }
        return $searchWhere;
    }

    protected function getWhere(){
        return array_merge($this->getParamWhere(),$this->getSearchWhere());
    }
}